package com.ruoyi.common.utils.uniquekey;

import java.util.Random;
import java.util.UUID;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/5 17:08
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
public final class IdGererateFactory implements IIdGenerator {
	private IIdGenerator idGenerator;

	private IdGererateFactory() {
	}

	public IdGererateFactory(Long workerId) {
		this.idGenerator = new WorkerIdGenerator(workerId);
	}

	public IdGererateFactory(Long workerId, Long dataCenterId) {
		if (dataCenterId == null) {
			this.idGenerator = new WorkerIdGenerator(workerId);
		} else {
			// 待完善
//			this.idGenerator = new SnowflakeIdGenerator(workerId, dataCenterId);

		}

	}

	public Long nextId() {
		return this.idGenerator.nextId();
	}

	// 生成随机数
	public String makeNum(int a){
		Random random = new Random();
		//生成一个随机数[0,9999999) 末尾加上“”表明是字符串类型
		String num = random.nextInt(99999) + "";
		StringBuffer stringBuffer = new StringBuffer();
		//不足a位用0填充
		for (int i = 0; i < a - num.length(); i++) {
			stringBuffer.append("0");
		}
		num = stringBuffer.toString() + num;
		return num;
	}

	/**
	 *
	 * 获取指定长度id
	 *
	 * @return str
	 */
	public String getUUID(int len)
	{
		if(0 >= len) {
			return null;
		}
		String uuid = UUID.randomUUID().toString().replace("-", "");
		StringBuffer str = new StringBuffer();

		for(int i = 0; i < len; i++) {
			str.append(uuid.charAt(i));
		}
		return str.toString();
	}

}
